//
//  HNHomeController.m
//  shezhan
//
//  Created by admin on 16/8/13.
//  Copyright © 2016年 haoniu. All rights reserved.
//

#import "HNHomeController.h"

@interface HNHomeController ()<UIWebViewDelegate>{
    UINavigationBar * _navBar;
    UILabel * _navBarLine;
}

@end

@implementation HNHomeController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    NSString * _loginName = [NSString stringWithFormat:@"%@",[AppConfig getUserInfoByKey:@"hnLoginName"]];
    NSLog(@"====hnLoginName:%@",[AppConfig getUserInfoByKey:@"hnLoginName"]);
    _webViewUrl = [NSMutableString stringWithFormat:@"%@%@?hnLoginName=%@",url_Base,url_Home,_loginName];
    _webViewUrl = [NSMutableString stringWithFormat:@"%@",[_webViewUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    NSURLRequest * _req = [NSURLRequest requestWithURL:[NSURL URLWithString:_webViewUrl]];
    _webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 20, mainScreenSize.width, mainScreenSize.height-70)];
    //_webView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleBottomMargin;
    _webView.autoresizesSubviews = YES;
    _webView.delegate = self;
    //_webView.scrollView.delegate = self;
    [_webView setBackgroundColor:[AppConfig colorWithHexString:@"#ececec"]];
    _webView.scrollView.bounces = YES;
    [self.view addSubview:_webView];
    [_webView loadRequest:_req];
    
    
    
    //[self createUI];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma -mark WebView
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
    NSLog(@"WebView erros is %@",error);
    if ([error code] == NSURLErrorCancelled) {
        return;
    }
    // 应用场景:加载从服务器上下载的文件,例如pdf,或者word,图片等等文件
    NSString *filePath = [[NSBundle mainBundle]pathForResource:@"error" ofType:@"html"];
    NSString *htmlString = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
    [webView loadHTMLString:htmlString baseURL:[NSURL URLWithString:filePath]];
}
-(void)webViewDidFinishLoad:(UIWebView *)webView{
    // Disable user selection
    [webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.style.webkitUserSelect='none';"];
    // Disable callout
    [webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.style.webkitTouchCallout='none';"];
}
- (BOOL)webView: (UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    NSString *_str = [[request URL] absoluteString];
    NSLog(@"=============123:%@",request);
    NSURL * url = [request URL];
    NSLog(@"===url:%@,%@",url,[url description]);
    if ([[_str substringToIndex:5] isEqualToString:@"file:"]) {
        return YES;
    }else if ([_str isEqualToString:_webViewUrl]) {
        //_webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 20, mainScreenSize.width, self.view.frame.size.height-70)];
        //创建一个消息对象
        NSNotification * notice = [NSNotification notificationWithName:@"HNMainControllerNotification" object:nil userInfo:@{@"tabbarShow":@"1",@"controller":@"home"}];
        //发送消息
        [[NSNotificationCenter defaultCenter]postNotification:notice];
        return YES;
    }
    //_webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 20, mainScreenSize.width, self.view.frame.size.height-20)];
    //创建一个消息对象
    NSNotification * notice = [NSNotification notificationWithName:@"HNMainControllerNotification" object:nil userInfo:@{@"tabbarShow":@"0",@"controller":@"home"}];
    //发送消息
    [[NSNotificationCenter defaultCenter]postNotification:notice];
    return YES;
}

//联系商家
-(void)HNHomeJSExportContactMerchantResult:(NSString *)merchantPhone{
    
    
    
}
//-(void)createUI{
//    //ScrollView
//    _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, mainScreenSize.width, mainScreenSize.height-50)];
//    [_scrollView setContentSize:CGSizeMake(mainScreenSize.width, mainScreenSize.height*2)];
//    _scrollView.delegate = self;
//    [self.view addSubview:_scrollView];
//    
//    //NavBar
//    _navBar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, mainScreenSize.width, 64)];
//    [_navBar setBackgroundColor:[UIColor colorWithWhite:1 alpha:0]];
//    [self.view addSubview:_navBar];
//    [self.view bringSubviewToFront:_navBar];
//    for (UIView * view in _navBar.subviews) {
//        NSLog(@"=====UINavigationBar:%@",[view class]);
//        [view removeFromSuperview];
//    }
//    _navBarLine = [[UILabel alloc]initWithFrame:CGRectMake(0, 63.5, mainScreenSize.width, 0.5)];
//    [_navBarLine setBackgroundColor:[[AppConfig colorWithHexString:@"#cacaca"] colorWithAlphaComponent:0]];
//    [_navBar addSubview:_navBarLine];
//    
//    //Banner
//    _banner = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, mainScreenSize.width, mainScreenSize.width*3/4)];
//    [_banner setImage:[UIImage imageNamed:@"640*960"]];
//    [_scrollView addSubview:_banner];
//    
//    //类型View
//    _typeArray = [NSMutableArray arrayWithObjects:@"闲置",@"寄卖速卖",@"维修养护",@"线下门店", nil];
//    _typeView = [[UIView alloc]initWithFrame:CGRectMake(0, mainScreenSize.width*3/4, mainScreenSize.width, 110)];
//    [_typeView setBackgroundColor:[UIColor whiteColor]];
//    for (int i = 0; i<_typeArray.count; i++) {
//        UIButton * button = [[UIButton alloc]initWithFrame:CGRectMake(i*mainScreenSize.width/4, 0, mainScreenSize.width/4, 110)];
//        [button setTitle:_typeArray[i] forState:UIControlStateNormal];
//        [button setTitleColor:[AppConfig colorWithHexString:@"#686868"] forState:UIControlStateNormal];
//        [button.titleLabel setFont:[UIFont systemFontOfSize:14]];
//        [button setBackgroundColor:[UIColor whiteColor]];
//        [_typeView addSubview:button];
//    }
//    
//    [self.scrollView addSubview:_typeView];
//}
//#pragma -mark ScrollView 监测contentOffset.y的变化
//-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
//    CGFloat scrollOrgY = scrollView.contentOffset.y;
//    CGFloat navbarAlpha = scrollOrgY/(mainScreenSize.width*3/4-64);
//    [_navBar setBackgroundColor:[UIColor colorWithWhite:1 alpha:navbarAlpha]];
//    [_navBarLine setBackgroundColor:[[AppConfig colorWithHexString:@"#cacaca"] colorWithAlphaComponent:navbarAlpha]];
//    NSLog(@"scrollViewDidScroll:%d,%0.2f",(int)scrollOrgY,navbarAlpha);
//}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
